/*
 * Administrador_de_fincas.java
 *
 * Created on April 19, 2008, 3:26 PM
 */

package GUI;
import ReglasNegocio.*;
import javax.swing.*;
import AccesoDatos.*;
import ModelosDatos.*;
import javax.swing.table.*;

/**
 *
 * @author  developer
 */
public class frmAdministrador extends javax.swing.JFrame {
    
    /** Creates new form Administrador_de_fincas */
    public frmAdministrador() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.comboMandadores.setModel(this.modeloMandadores());
        this.tablaFincas.setModel(this.modeloFincas());
        TableColumn ColMandador=this.tablaFincas.getColumnModel().getColumn(4);
        ColMandador.setCellEditor(new DefaultCellEditor(this.comboMandadores));
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PopupMenuFinca = new javax.swing.JPopupMenu();
        ItemEliminar = new javax.swing.JMenuItem();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtDireccion = new javax.swing.JTextField();
        txtManzanas = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        comboMandadores = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaFincas = new javax.swing.JTable();

        ItemEliminar.setText("Eliminar Finca");
        ItemEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ItemEliminarActionPerformed(evt);
            }
        });
        PopupMenuFinca.add(ItemEliminar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel4.setText("Direcccion");

        jLabel5.setText("Manzanas");

        jLabel6.setText("Mandador");

        jLabel3.setText("Nombre");

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 24));
        jLabel1.setText("Agregar Finca");

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtManzanas, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(15, 15, 15))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(txtNombre)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboMandadores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1))
                .addContainerGap(120, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtManzanas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboMandadores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Registrar Finca", jPanel1);

        tablaFincas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablaFincas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tablaFincasMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tablaFincas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void MostrarMenu(java.awt.event.MouseEvent e) {
        if (this.PopupMenuFinca.isPopupTrigger(e)) {
            this.PopupMenuFinca.show(e.getComponent(), e.getX(), e.getY());
        }
    }
    
    private DatosFinca modeloFincas() {
        DatosFinca modelo=new DatosFinca();
        
        modelo.addColumn("Id");
        modelo.addColumn("Nombre");
        modelo.addColumn("Direccion");
        modelo.addColumn("Manzanas");
        modelo.addColumn("Mandador");
        
        String consulta="select * from Finca";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        
        Vista v=con.crearVista(consulta);
        
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long id=(Long)v.getRegistro(i, 0);
                Dato idFinca=new Dato("id",String.valueOf(id));
                Finca fa=new Finca(idFinca);
                String[] fila={fa.getIdentificador().getValor(),fa.getNombre(),fa.getDireccion(),String.valueOf(fa.getManzanas()),fa.getMandador().toString()};
                modelo.addRow(fila);
            }
        }
        
        return modelo;
    }

    private DefaultComboBoxModel modeloMandadores() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        
        String buscar="select * from Mandador";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        
        Vista v=con.crearVista(buscar);
        
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long idMandador=(Long)v.getRegistro(i, 0);
                Dato id=new Dato("id",String.valueOf(idMandador));
                Mandador m=new Mandador(id);
                modelo.addElement(m);
            }
        }
        return modelo;
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        
        Finca f=new Finca();
        f.setNombre(this.txtNombre.getText());
        f.setDireccion(this.txtDireccion.getText());
        f.setManzanas(Double.valueOf(this.txtManzanas.getText()));
        Mandador mandadorElegido=(Mandador) this.comboMandadores.getSelectedItem();
        f.setMandador(mandadorElegido);
        f.Crear();
        JOptionPane.showMessageDialog(this, "Se ha registrado la finca "+f.getNombre());
    }//GEN-LAST:event_jButton1ActionPerformed

private void tablaFincasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaFincasMouseReleased
// TODO add your handling code here:
    this.MostrarMenu(evt);
}//GEN-LAST:event_tablaFincasMouseReleased

private void ItemEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ItemEliminarActionPerformed
// TODO add your handling code here:
    try {
        int fila=this.tablaFincas.getSelectedRow();
        Dato idFinca=new Dato("id",(String)this.tablaFincas.getModel().getValueAt(fila, 0));
        Finca f=new Finca(idFinca);
        int decision=JOptionPane.showConfirmDialog(this, "Esta seguro de eliminar este registro");
        if (decision==0) {
            f.Eliminar();
            ((DefaultTableModel)this.tablaFincas.getModel()).removeRow(fila);
        }
    }
    catch(Exception ex) {
        JOptionPane.showMessageDialog(this, "No ha seleccionado una Finca");
    }
}//GEN-LAST:event_ItemEliminarActionPerformed

    
    public static void main(String[] args) {
        new frmAdministrador().setVisible(true);
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ItemEliminar;
    private javax.swing.JPopupMenu PopupMenuFinca;
    private javax.swing.JComboBox comboMandadores;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tablaFincas;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtManzanas;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
    
}
